/*
 *  Author: Sinclair Ross
 *  Date: 29/04/2014
 *  Description: 
 *
 *
 *
 */

package com.forbiddendesert.master;

import java.util.Iterator;
import java.util.Vector;

import com.forbiddendesert.logic.GameLogic;
import com.forbiddendesert.tiles.Tile;
import com.forbiddendesert.characters.Character;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameView extends SurfaceView implements SurfaceHolder.Callback
{
	private Vector<Character> mCharacters;
	private Vector<Tile> mTiles;
	private GameLogic mGameLogic;
	
	public GameView(Context context, Handler handler, RendererPacket rendererPacket, GameLogic gameLogic) 
	{
		super(context);
		
		mGameLogic = gameLogic;
		mCharacters = rendererPacket.GetCharacters();
		mTiles = rendererPacket.GetTiles();
		
		getHolder().addCallback(this);
	}
	
	@Override
	public void onDraw(Canvas canvas)
	{
		canvas.drawARGB(0, 0, 0, 255);		

		for(Iterator<Tile> iterator = mTiles.iterator(); iterator.hasNext();)
        {
            iterator.next().draw(canvas);
        }
		
		for(Iterator<Character> iterator = mCharacters.iterator(); iterator.hasNext();)
        {
            iterator.next().draw(canvas);
        }
		
		invalidate();
	}
	
	public boolean onTouchEvent(MotionEvent e) 
	{
		 switch (e.getAction()) 
		 {
		 	case MotionEvent.ACTION_DOWN : 
		 		
		 		
		 		 mGameLogic.detectedTouch(e.getX(), e.getY());
		 		 

		 		//mCharacters.firstElement().getGear().firstElement().useGear(null);
		 		//mCharacters.firstElement().checkSurroundingTiles();
		 
				break;
		 }
	
		return true;
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) 
	{

	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) 
	{
		setWillNotDraw(false);
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) 
	{
		
	}
}
